package com.erp.module.crm.addCrm

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.activityKView.CrmFKView
import com.erp.bean.ClientDataBean
import com.erp.module.crm.xXCrm.FragmentClientone
import com.erp.util.Httputil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_addcrm.*
import kotlinx.android.synthetic.main.header_knoya.*
import kotlinx.android.synthetic.main.header_knoya.view.*

/**
 * Created by Administrator on 2018/6/4/004.
 * 作者：刘洋
 */
class AddCrmActivity : BaseActivity(), View.OnClickListener ,CrmFKView{


    val a = "AddCrmActivity"
    lateinit var fragmengt : FragmentClientone
    lateinit var transaction: FragmentTransaction
    lateinit var mFragmentMan: FragmentManager
    override fun getLayoutId(): Int {
        activityname=a
        return R.layout.activity_addcrm
    }

    override fun initCreate() {
        initView()
        initData()
    }
    private fun initView() {
        Header.iv_header_left.visibility=View.VISIBLE
        iv_header_left.setOnClickListener(this)
        tv_header_title.text="创建新客户"
        tv_header_right.visibility=View.VISIBLE
        tv_header_right.setOnClickListener(this)
        tv_header_right.text="确定"
    }
    private fun initData() {
        mFragmentMan = supportFragmentManager
        transaction = mFragmentMan.beginTransaction()
        fragmengt = FragmentClientone(ClientDataBean(), this, 1)
        fragmengt.addUpdata(this);
        transaction.add(R.id.fragment, fragmengt).commit()
    }
    lateinit var updatedialog : AlertDialog
    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> {
                finish()
            }
            tv_header_right -> {
                val bean=fragmengt.getBean()
                if(!Myutil.isTel(bean.tel)){
                    Myutil.MyTost("输入正确的电话号码")
                    return
                }
                updatedialog= Myutil.showupdatedialog(this@AddCrmActivity, "正在新建客户", false)
                Httputil.addclient(this@AddCrmActivity,bean,object : OnUserDataInterface {
                    override fun onUserData(data: String?) {
                        Log.d("TAAAG","data="+data)
                        updatedialog.dismiss()
                        if(data==null){
                            return
                        }
                        val i = Intent()
                        i.putExtra("ok",true)
                        setResult(Activity.RESULT_OK, i)
                        finish()
                    }

                    override fun onNot(throwable: String?) {
                        updatedialog.dismiss()
                        Log.d("AddCrmActivity","throwable="+throwable)
                    }

                })
            }
        }
    }
    override fun updata(s: String) {
        val bean=fragmengt.getBean()
        if(!Myutil.isTel(bean.tel)){
            Myutil.MyTost("输入正确的电话号码")
            return
        }
        updatedialog= Myutil.showupdatedialog(this@AddCrmActivity, "正在新建客户", false)
        Httputil.addclient(this@AddCrmActivity,bean,object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                Log.d("TAAAAAAG","data="+data)
                updatedialog.dismiss()
                if(data==null){
                    return
                }
                val i = Intent()
                i.putExtra("ok",true)
                setResult(Activity.RESULT_OK, i)
                finish()
            }

            override fun onNot(throwable: String?) {
                updatedialog.dismiss()
                Log.d("AddCrmActivity","throwable="+throwable)
            }

        })
    }

}